//
//  TempLovExtesion.swift
//  ANOA
//
//  Created by 邱仙凯 on 2023/7/12.
//  Copyright © 2023 zwb. All rights reserved.
//  lov   下拉选    跟随接口

import UIKit

extension ConfigMenuForm  {
   
    // 下拉选
    func showXLSelect(onexx:TemplateModel,indexPath:IndexPath?) {
        
        // 下拉选多选
        if onexx.is_multichoice == "1" {
            
                
                var allArr = [(nameAndModel<SegmenttData>,Bool)]()
            for jsonone in onexx.selectDataArr {
                let one_x = (nameAndModel(shows: [jsonone.nm.unwrap], model: jsonone),false)
                allArr.append(one_x)
            }
           
                let cle:([Any])->() = { arr in
                    let realArr = arr as! [nameAndModel<SegmenttData>]
                    let ids = realArr.compactMap { $0.model.id }.joined(separator: ",")
                    let nms = realArr.compactMap { $0.model.nm }.joined(separator: ",")

                    
                    if indexPath.isSome {
                        self.detHandler[self.cDIndex][indexPath!.section].dic[onexx.selectDataBack!.idBack.unwrap] = ids
                        self.detHandler[self.cDIndex][indexPath!.section].dic[onexx.selectDataBack!.nmBack.unwrap] = nms
                        self.reloadDetData(indexPath:indexPath)
                        
                    } else {
                        self.main[onexx.selectDataBack!.idBack.unwrap] = ids
                        self.main[onexx.selectDataBack!.nmBack.unwrap] = nms
                        
                        self.configTempDataSource()
                         
                        self.continueUrl(onexx: onexx)
                    }
                    
                    
                    
                }
                let showC:((GlobalTableViewCell, Any) -> ()) = { cell, model in
                    
                    var contArr = [(String,String?)]()
                    let realM = model as! nameAndModel<SegmenttData>
                    if  realM.shows.count > 0 {
                        contArr.append(("",realM.shows.first!))
                    } else {
                        contArr.append(("","[数据有误]"))
                    }
                     
                    cell.setDataSource(contentArr: contArr)
                    
                }
                let filterC:((Any, String) -> (Bool)) = { model, keyword -> Bool in
                   
                    let realM = model as! nameAndModel<SegmenttData>
                    if  realM.shows.count > 0 {
                        return realM.shows.first!.contains(keyword)
                    } else {
                        return true
                    }
                    
                }
                let vc = KSMultiSelectUtil.initWithNav(title: "选择", allArr: allArr ,backClosure: cle,kSShowCell: showC , filterClosure: filterC)
                self.present_FullScreen(to: vc)
           
        } else {
            var allArr = [nameAndModel<SegmenttData>]()
            for jsonone in onexx.selectDataArr {
                let one_x = nameAndModel(shows: [jsonone.nm.unwrap], model: jsonone)
                allArr.append(one_x)
            }
            
            let cle:(nameAndModel<SegmenttData>)->() = { one in
                
                
                if indexPath.isSome {
                    self.detHandler[self.cDIndex][indexPath!.section].dic[onexx.selectDataBack!.idBack.unwrap] = one.model.id
                    self.detHandler[self.cDIndex][indexPath!.section].dic[onexx.selectDataBack!.nmBack.unwrap] = one.model.nm
                    self.reloadDetData(indexPath:indexPath)
                    
                } else {
                    self.main[onexx.selectDataBack!.idBack.unwrap] = one.model.id
                    self.main[onexx.selectDataBack!.nmBack.unwrap] = one.model.nm
                    
                    self.configTempDataSource()
                     
                    self.continueUrl(onexx: onexx)
                }
                
            }
            
            let vc = KSSingleSelectUtil.initWithNav(title: "选择", allArr: allArr, backClosure: cle)
            let topViewController = RouterUtils.currentTopViewController()
            topViewController!.present_FullScreen(to: vc)
        }
        
        
    }
}
